{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Convert 1D Array Into 2D Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: construct2DArray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #将一维数组转变成二维数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的一维整数数组&nbsp;<code>original</code>&nbsp;和两个整数&nbsp;<code>m</code>&nbsp;和&nbsp;&nbsp;<code>n</code>&nbsp;。你需要使用&nbsp;<code>original</code>&nbsp;中&nbsp;<strong>所有</strong>&nbsp;元素创建一个&nbsp;<code>m</code>&nbsp;行&nbsp;<code>n</code>&nbsp;列的二维数组。</p>\n",
    "\n",
    "<p><code>original</code>&nbsp;中下标从 <code>0</code>&nbsp;到 <code>n - 1</code>&nbsp;（都 <strong>包含</strong> ）的元素构成二维数组的第一行，下标从 <code>n</code>&nbsp;到 <code>2 * n - 1</code>&nbsp;（都 <strong>包含</strong>&nbsp;）的元素构成二维数组的第二行，依此类推。</p>\n",
    "\n",
    "<p>请你根据上述过程返回一个<em>&nbsp;</em><code>m x n</code>&nbsp;的二维数组。如果无法构成这样的二维数组，请你返回一个空的二维数组。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img src=\"https://assets.leetcode.com/uploads/2021/08/26/image-20210826114243-1.png\" style=\"width: 500px; height: 174px;\">\n",
    "<pre><b>输入：</b>original = [1,2,3,4], m = 2, n = 2\n",
    "<b>输出：</b>[[1,2],[3,4]]\n",
    "<strong>解释：\n",
    "</strong>构造出的二维数组应该包含 2 行 2 列。\n",
    "original 中第一个 n=2 的部分为 [1,2] ，构成二维数组的第一行。\n",
    "original 中第二个 n=2 的部分为 [3,4] ，构成二维数组的第二行。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>original = [1,2,3], m = 1, n = 3\n",
    "<b>输出：</b>[[1,2,3]]\n",
    "<b>解释：</b>\n",
    "构造出的二维数组应该包含 1 行 3 列。\n",
    "将 original 中所有三个元素放入第一行中，构成要求的二维数组。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>original = [1,2], m = 1, n = 1\n",
    "<b>输出：</b>[]\n",
    "<strong>解释：\n",
    "</strong>original 中有 2 个元素。\n",
    "无法将 2 个元素放入到一个 1x1 的二维数组中，所以返回一个空的二维数组。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>original = [3], m = 1, n = 2\n",
    "<b>输出：</b>[]\n",
    "<strong>解释：</strong>\n",
    "original 中只有 1 个元素。\n",
    "无法将 1 个元素放满一个 1x2 的二维数组，所以返回一个空的二维数组。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= original.length &lt;= 5 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>1 &lt;= original[i] &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 &lt;= m, n &lt;= 4 * 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [convert-1d-array-into-2d-array](https://leetcode.cn/problems/convert-1d-array-into-2d-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [convert-1d-array-into-2d-array](https://leetcode.cn/problems/convert-1d-array-into-2d-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,4]\\n2\\n2', '[1,2,3]\\n1\\n3', '[1,2]\\n1\\n1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if m * n != len(original):\n",
    "            return []\n",
    "        ret = [[] for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            ret[i].extend(original[i * n: (i+1) * n])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        origin = len(original)\n",
    "        if origin != m * n:\n",
    "            return []\n",
    "        \n",
    "        col, row = 0, 0\n",
    "        res = [[] for i in range(m)]\n",
    "        for i in range(origin):\n",
    "            res[row].append(original[i])\n",
    "            col += 1\n",
    "            if col == n:\n",
    "                col = 0\n",
    "                row +=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        size = len(original)\n",
    "        if size != m*n:\n",
    "            return []\n",
    "        L = [[original[i*n+j] for j in range(n)] for i in range(m)]\n",
    "        return L\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) != m * n:\n",
    "            return []\n",
    "        res = []\n",
    "        raw = []\n",
    "        rawSize = 0\n",
    "        for idx in range(len(original)):\n",
    "            raw.append(original[idx])\n",
    "            rawSize += 1\n",
    "            if rawSize == n:\n",
    "                res.append(raw)\n",
    "                rawSize = 0\n",
    "                raw = []\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        graph = [[0 for _ in range(n)] for _ in range(m)]\n",
    "        N = len(original)\n",
    "        if N != m * n:\n",
    "            return []\n",
    "        for j in range(N):\n",
    "            k = j%n\n",
    "            i = j//n\n",
    "            print(i,k,original[j])\n",
    "            graph[i][k] = original[j]\n",
    "\n",
    "        return graph\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) != m * n:\n",
    "            return []\n",
    "        ret = [[0 for i in range(n)] for j in range(m)]\n",
    "        index = 0\n",
    "        for raw in range(m):\n",
    "            for col in range(n):\n",
    "                ret[raw][col] += original[index]\n",
    "                index += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original)!=n*m:\n",
    "            return []\n",
    "        res=[]\n",
    "        it=iter(original)\n",
    "        for i in range(m):\n",
    "            res.append([])\n",
    "            for j in range(n):\n",
    "\n",
    "                res[i].append(next(it))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        l=[]\n",
    "        if len(original)!=m*n:\n",
    "            return l\n",
    "        for i in range(len(original)):\n",
    "            if i%n==0:\n",
    "                l.append([])\n",
    "            l[i//n].append(original[i])\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        on = len(original)\n",
    "        if on != m * n:\n",
    "            return []\n",
    "        ans = []\n",
    "        for idx in range(on):\n",
    "            if idx % n == 0:\n",
    "                ans.append([])\n",
    "            ans[-1].append(original[idx])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        #  输入：original = [1,2,3,4], m = 2, n = 2\n",
    "        # 输出：[[1,2],[3,4]]\n",
    "        #  输入：original = [1,2,3], m = 1, n = 3\n",
    "        # 输出：[[1,2,3]]\n",
    "        #  输入：original = [1,2], m = 1, n = 1\n",
    "        # 输出：[]\n",
    "        count = len(original)\n",
    "        if count != m * n: return []\n",
    "        ans = [[0 for _ in range(n)] for _ in range(m)]\n",
    "        k = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                ans[i][j] = original[k]\n",
    "                k += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if m*n!=len(original):\n",
    "            return []\n",
    "        res=[None]*m\n",
    "        for i in range(m):\n",
    "            res[i]=original[i*n:i*n+n]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        # 边界\n",
    "        if m * n != len(original):\n",
    "            return []\n",
    "        \n",
    "        res = [[] for i in range(m)]\n",
    "        # 一般式: [n*i, n*(i+1))\n",
    "        for i in range(m):\n",
    "            res[i] = original[n*i:n*(i+1)]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        ans=[[0 for i in range(n)] for j in range(m)]\n",
    "        x=0\n",
    "        if len(original)!=m*n:\n",
    "            return []\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                ans[i][j]=original[x]\n",
    "                x+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if m*n != len(original):\n",
    "            return []\n",
    "        res = [[] for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            start = i*n\n",
    "            end = start + n\n",
    "            res[i] = original[start:end]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        l = len(original)\n",
    "        if m*n != l:\n",
    "            return []\n",
    "\n",
    "        res = []\n",
    "        for i in range(m):\n",
    "            line = []\n",
    "            for j in range(n):\n",
    "                line.append(original[i*n+j])\n",
    "            res.append(line)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) == m * n:\n",
    "            l = []\n",
    "            for i in range(0, len(original), n):\n",
    "                l.append(list(original[i: i + n]))\n",
    "            return l\n",
    "        else:\n",
    "            return []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def construct2DArray(self, original, m, n):\n",
    "        t = len(original)\n",
    "        if m * n != t:\n",
    "            return []\n",
    "        out = [[0 for _ in range(n)] for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                out[i][j] = original[i * n + j]\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        a=[]\n",
    "        if m*n==len(original):\n",
    "            for i in range(m):\n",
    "                b=[]\n",
    "                b.extend(original[i*n:(i+1)*n])\n",
    "                a.append(b)\n",
    "            return a    \n",
    "        else:\n",
    "            return []    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        return [original[i: i + n] for i in range(0, len(original), n)] if len(original) == m * n else []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        matrix = []\n",
    "        if m * n == len(original):\n",
    "            for i in range(m):\n",
    "                matrix.append(original[i*n:(i+1)*n])\n",
    "        return matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if m*n != len(original):\n",
    "            return []\n",
    "        res = [[] for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            start = i*n\n",
    "            end = start + n\n",
    "            res[i] = original[start:end]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) != m*n:\n",
    "            return []\n",
    "\n",
    "\n",
    "\n",
    "        ret = [[0]*n for _ in range(m)]\n",
    "\n",
    "        # for i in range(len(original)):\n",
    "        #     row = i//n\n",
    "        #     col = i % n\n",
    "        #     ret[row][col] = original[i]\n",
    "        # return ret\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                ret[i][j] = original[i*n+j]\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) != m * n:\n",
    "            return []\n",
    "        \n",
    "        return [original[n*i:n*i+n] for i in range(m)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        length = len(original)\n",
    "        if length != m*n:\n",
    "            return []\n",
    "        result = []\n",
    "        for i in range(0,length,n):\n",
    "            result.append(original[i:i+n])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "                # 计算一维数组的长度\n",
    "        length = len(original)\n",
    "        \n",
    "        # 如果无法构成 m x n 的二维数组，返回空数组\n",
    "        if length != m * n:\n",
    "            return []\n",
    "\n",
    "        # 创建一个空的二维数组\n",
    "        result = [[0] * n for _ in range(m)]\n",
    "\n",
    "        # 填充二维数组\n",
    "        for i in range(length):\n",
    "            result[i // n][i % n] = original[i]\n",
    "\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original)!=m*n:\n",
    "            return []\n",
    "        res=[[0]*n for i in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                a=i*n+j\n",
    "                res[i][j]=original[a]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) != m * n:\n",
    "            return []\n",
    "        return [\n",
    "            original[i * n: (i + 1) * n]\n",
    "            for i in range(m)\n",
    "        ]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], n: int, m: int) -> List[List[int]]:\n",
    "        if len(original)!=m*n: return []\n",
    "        res=[]\n",
    "        for i in range(n):\n",
    "            res.append(original[i*m:i*m+m])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original)!=m*n:\n",
    "            return []\n",
    "        res=[[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                res[i][j]=original[i*n+j]\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) != m * n:\n",
    "            return []\n",
    "        return [original[n*i:n*i+n] for i in range(m)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) != m*n:\n",
    "            return []\n",
    "\n",
    "        ret = [[0]*n for _ in range(m)]\n",
    "\n",
    "        for i in range(len(original)):\n",
    "            row = i//n\n",
    "            col = i % n\n",
    "            ret[row][col] = original[i]\n",
    "        return ret\n",
    "        # for i in range(m):\n",
    "        #     for j in range(n):\n",
    "        #         ret[i][j] = original[i*n+j]\n",
    "        # return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        # import numpy as np\n",
    "        # if len(original) != m*n:\n",
    "        #     return []\n",
    "        # return np.array(original).reshape(m, n).tolist()\n",
    "        if len(original) != m*n:\n",
    "            return []\n",
    "        res = []\n",
    "        for i in range(m):\n",
    "            res.append(original[i * n:i * n+n])\n",
    "        return res\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        return [original[i: i + n] for i in range(0, len(original), n)] if len(original) == m * n else []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        k = len(original)\n",
    "        if k != m*n:\n",
    "            return []\n",
    "        ans = [[0]*n for _ in range(m)]\n",
    "        for i in range(k):\n",
    "            ans[i//n][i%n] = original[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if m * n != len(original): return []\n",
    "        res = []\n",
    "        curIndex = 0\n",
    "        for i in range(m):\n",
    "            res.append(original[curIndex:curIndex+n])\n",
    "            curIndex+=n\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) == m*n:\n",
    "            res = []\n",
    "            for i in range(m):\n",
    "                res.append(original[i*n:i*n+n])\n",
    "            return res\n",
    "        else:\n",
    "            return []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) != m * n:\n",
    "            return []\n",
    "        ans = []\n",
    "        for i in range(m):\n",
    "            ans.append(original[i * n: (i + 1) * n])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) != m*n:\n",
    "            return []\n",
    "        res = []\n",
    "        for i in range(m):\n",
    "            res.append(original[i*n:(i+1)*n])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        l = len(original)\n",
    "        if l != m * n:\n",
    "            return []\n",
    "        ans = []\n",
    "        for i in range(0, l, n):\n",
    "            ans.append(original[i:i+n])\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        ll = len(original)\n",
    "        if ll != m * n:\n",
    "            return []\n",
    "        else:\n",
    "            maxtric = [0 * n] * m\n",
    "            j = 0\n",
    "            for i in range(m):\n",
    "                maxtric[i] = original[j:j+n]\n",
    "                j = j + n\n",
    "            return maxtric"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        array = []\n",
    "        ln = len(original)\n",
    "        if m * n != ln:\n",
    "            return array\n",
    "        for i in range(0, m):\n",
    "            array.append(original[n * i: n * (i + 1)])\n",
    "        return array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        result = []\n",
    "        if m*n > len(original) or len(original) > m*n:\n",
    "            return result\n",
    "        else:\n",
    "            idx = 0\n",
    "            for i in range(m):\n",
    "                tmp = []\n",
    "                for j in range(n):\n",
    "                    tmp.append(original[idx])\n",
    "                    idx += 1\n",
    "                result.append(tmp)\n",
    "            return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        arr = [[0]*n for _ in range(m)]\n",
    "        if len(original) != m*n:\n",
    "            return []\n",
    "        else:\n",
    "            for i in range(m):\n",
    "                arr[i][:] = original[i*n:i*n+n]\n",
    "        return arr\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original)!=m*n:\n",
    "            return []\n",
    "        ans = []\n",
    "        for i in range(m):\n",
    "            ans.append(original[i*n:(i+1)*n])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        l = len(original)\n",
    "        res = []\n",
    "        if l != m*n:\n",
    "            return []\n",
    "        else:\n",
    "            for i in range(m):\n",
    "                res.append(original[i*n:(i+1)*n])\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if m * n != len(original):\n",
    "            return []\n",
    "        ans = []\n",
    "        for i in range(0,len(original),n):\n",
    "            ans.append(original[i:i+n])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "        if len(original) != m*n:\n",
    "            return ans\n",
    "        for i in range(m):\n",
    "            ans.append(original[i*n : (i+1)*n])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "\n",
    "        if not (len(original) == m * n):\n",
    "            return []\n",
    "\n",
    "        res = []\n",
    "\n",
    "        for i in range(m):\n",
    "            res.append(original[i*n:(i+1)*n])\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        return [[original[i * n + j] for j in range(n)] for i in range(m)] if m * n == len(original) else []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if m*n!=len(original):\n",
    "            return []\n",
    "        \n",
    "        res=[]\n",
    "        for i in range(0, len(original), n):\n",
    "            res.append(original[i:i+n])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        res=[]\n",
    "        if len(original)!=m*n:\n",
    "            return []\n",
    "        else:\n",
    "            for i in range(m):\n",
    "                x=original[i*n:(i+1)*n]\n",
    "                res.append(x)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        # import numpy as np\n",
    "        # if len(original) != m*n:\n",
    "        #     return []\n",
    "        # return np.array(original).reshape(m, n).tolist()\n",
    "        if len(original) != m*n:\n",
    "            return []\n",
    "        res = []\n",
    "        for i in range(m):\n",
    "            res.append(original[i * n:i * n+n])\n",
    "        return res\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        l = len(original)\n",
    "        if l!=m*n:\n",
    "            return []\n",
    "        return [original[i*n:(i+1)*n] for i in range(m)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        return [original[i:i+n] for i in range(0,len(original),n)] if len(original) == m*n else []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if n * m != len(original) :\n",
    "            return [] \n",
    "        ans = []\n",
    "        cnt = 0 \n",
    "        for i in range(0,len(original),n):\n",
    "            tmp = []\n",
    "            for j in range(i,i+n) :\n",
    "                tmp.append(original[j])\n",
    "            ans.append(tmp)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if not m*n == len(original):return []\n",
    "        \n",
    "        return [original[i:i+n] for i in range(0,len(original),n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) != m * n:\n",
    "            return []\n",
    "        res = [[0] * n for _ in range(m)]\n",
    "        for i, x in enumerate(original):\n",
    "            row = i // n\n",
    "            col = i % n\n",
    "            res[row][col] = x\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original)!=m*n:return []\n",
    "        index = 0\n",
    "        res = [[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            res[i] = original[index:index+n]\n",
    "            index += n\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if not m*n == len(original):return []\n",
    "        return [original[i:i+n] for i in range(0,len(original),n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        length=len(original)\n",
    "        if((m*n)!=length):\n",
    "            return []\n",
    "        res=[[0]*n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            res[i][:]=original[i*n:i*n+n]\n",
    "        return res\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if len(original) != m * n:\n",
    "            return []\n",
    "        \n",
    "        res = []\n",
    "        start = 0\n",
    "        for i in range(m):\n",
    "            res.append(original[start:start+n])\n",
    "            start = start + n\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        x=len(original)\n",
    "        if m*n != x:\n",
    "            return []\n",
    "        else:\n",
    "            return [original[i:i+n] for i in range(0,x,n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        return [[original[i * n + j] for j in range(n)] for i in range(m)] if m * n == len(original) else []\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        ans = list()\n",
    "        if m * n != len(original): return ans\n",
    "        for x in range(m):\n",
    "            ans.append([])\n",
    "            for y in range(n):\n",
    "                ans[-1].append(original[x * n + y])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if m * n !=  len(original):\n",
    "            return []\n",
    "        return [[original[i * n + j] for j in range(n)] for i in range(m)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        N = len(original)\n",
    "        if N != m * n:\n",
    "            return []\n",
    "        res = [[0 for _ in range(n)] for _ in range(m)]\n",
    "        for i in range(N):\n",
    "            r = i // n\n",
    "            c = i % n\n",
    "            res[r][c] = original[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        length = len(original)\n",
    "        if length != m * n:\n",
    "            return []\n",
    "        return [[ original[j + i * n] for j in range(n)] for i in range(m)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:\n",
    "        if m * n !=  len(original):\n",
    "            return []\n",
    "        nums = iter(original)\n",
    "        return [[next(nums) for _ in range(n)] for _ in range(m)]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
